<?php 
/*
* StiPHPcms, A web Content management System made with PHP/MySQL
* JordSti : jord52@gmail.com
* Version : 0.0.2
*
* Copyright (C) 2009  jord52@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


function StiCodeHTMLFormBar($fieldName)
{
	$html = '<div class="sticodebar">
	<p>StiCode</p>
	<input type="button" value="URL" onclick="bbCodeMarker(\'[url]\',\'[/url]\',\''.$fieldName.'\');">
	<input type="button" value="Image" onclick="bbCodeMarker(\'[img]\',\'[/img]\',\''.$fieldName.'\');">
	<input type="button" value="Quote" onclick="bbCodeMarker(\'[quote]\',\'[/quote]\',\''.$fieldName.'\');">
	<input type="button" value="h1" onclick="bbCodeMarker(\'===\',\'===\',\''.$fieldName.'\');">
	<input type="button" value="h2" onclick="bbCodeMarker(\'====\',\'====\',\''.$fieldName.'\');">
	<input type="button" value="h3" onclick="bbCodeMarker(\'=====\',\'====\',\''.$fieldName.'\');">
	<input type="button" value="h4" onclick="bbCodeMarker(\'======\',\'=====\',\''.$fieldName.'\');">
	<input type="button" value="Page" onclick="bbCodeMarker(\'[page]\',\'[/page]\',\''.$fieldName.'\');">
	</div>';
	return $html;
}

function parseStiCode($string)
{
	//Image Maximum size
	$max_x = 400;
	$max_y = 400;
	
	$string = stripslashes($string);
	$string = nl2br($string);
	
	$string = preg_replace('#\[b\](.+)\[/b\]#isU', '<strong>$1</strong>', $string);
	$string = preg_replace('#\[i\](.+)\[/i\]#isU', '<em>$1</em>', $string);
	$string = preg_replace('#\[color=(red|green|blue|yellow|purple|olive)\](.+)\[/color\]#isU', '<span style="color:$1">$2</span>', $string);
	
	$string = preg_replace('#\[url\](.+)\[/url\]#isU', '<a href="$1">$1</a>', $string);
	
	$string = preg_replace('#\[url=(.+)\](.+)\[/url\]#isU', '<a href="$1">$2</a>', $string);
	
	$string = preg_replace('#\[page\](.+)\[/page\]#isU', '<a href="page.php?p=$1">$1</a>', $string);
	
	$string = preg_replace('#\[page=(.+)\](.+)\[/page\]#isU', '<a href="page.php?p=$1">$2</a>', $string);
	
	$string = preg_replace('#\[quote\](.+)\[/quote\]#isU', '<div class="quote">$1</div>', $string);
	
	preg_match_all('#\[img\](.+)\[/img\]#isU',$string,$matches);
	
	$img = $matches[1];
	
	foreach($img as $key => $url)
	{
		$dis = getimagesize($url);
		
		$img_w = $dis[0];
		$img_h = $dis[1];
		
		if($img_w > $max_x || $img_h > $max_y)
		{
			if($img_w == $img_h)
			{
				$img_w = $max_x;
				$img_h = $max_y;
			}
			else if($img_w > $img_h)
			{
				$img_h = ($max_x*$img_h)/$img_w;
				$img_w = $max_x;
			}
			else if($img_h > $img_w)
			{
				$img_w = ($max_y*$img_w)/$img_h;
				$img_h = $max_y;
			}
		}
		
		$string = preg_replace('#\[img\]'.$url.'\[/img\]#isU', '<a href="'.$url.'" target="_blank"><img src="'.$url.'" width="'.$img_w.'" height="'.$img_h.'"></a>', $string);
	}
	
	$string = preg_replace('#======(.+)======#isU', '<h5>$1</h5>', $string);
	
	$string = preg_replace('#=====(.+)=====#isU', '<h4>$1</h4>', $string);
	
	$string = preg_replace('#====(.+)====#isU', '<h3>$1</h3>', $string);
	
	$string = preg_replace('#===(.+)===#isU', '<h2>$1</h2>', $string);
	
	$string = preg_replace('#\[p\](.+)\[/p\]#isU', '<p>$1</p>', $string);
	
	return $string;
}

?>